package com.ilas.service.impl;

import com.ilas.config.DynamicDataSource;
import com.ilas.dao.TenantInfoDao;
import com.ilas.entity.TenantInfo;
import com.ilas.service.ITenantInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 旧版-租户信息
 *
 * @author luzck
 * @date 2021/4/1 16:43
 */
@Service
public class TenantInfoServiceImpl implements ITenantInfoService {

    @Resource
    private TenantInfoDao tenantInfoDao;

    @Resource
    private DynamicDataSource dynamicDataSource;

    @Override
    public List<TenantInfo> findList() {
        return tenantInfoDao.findList();
    }

    @Override
    public TenantInfo add(TenantInfo tenantInfo) {
        int i = tenantInfoDao.insert(tenantInfo);
        if (i > 0) {
            //加载到动态数据源列表中
            dynamicDataSource.setDataSources(tenantInfo);

            return tenantInfo;
        }
        return null;
    }

    /**
     * 检查租户编号是否存在
     * @param tenantId 租户编号
     * @return 是否存在
     */
    @Override
    public boolean isExist(String tenantId) {
        TenantInfo tenantInfo = tenantInfoDao.getByTenantCode(tenantId);

        return tenantInfo != null;
    }

}
